package com.oss.view;

import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.DetachedCriteria;
import org.oss.client.dto.OssSecurityResponse;
import org.oss.util.JsonMapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.ServiceSignature;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.internal.OSSHeaders;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.opensymphony.xwork2.ActionSupport;
import com.oss.base.dao.IBaseDao;
import com.oss.service.IUserService;
import com.oss.service.TestService;
import com.oss.service.impl.OssSTSService;
import com.oss.vo.SysUser;
import com.oss.vo.TestVo;
import com.sun.tools.internal.ws.processor.model.Request;

@Results({
	@Result(name="a",   type = "dispatcher", location = "/error.jsp"),
})


public class TestAction extends ActionSupport{
	Logger logger = Logger.getLogger(this.getClass());
	public String req;
	@Autowired
	IBaseDao baseDao;
	@Autowired
	IUserService userService;
	@Action("a")
	public void test(){
		try {
			System.out.println("============");
//			TestVo t = new TestVo();
//			t.setEmail("1442342");
//			t.setWorkername("sfsdfds");
//			t.setEmployedDates(new Date());
//			t.setDepartment("AAA");
			SysUser s = new SysUser();
			s.setAppName("fs");
			s.setAppPath("ff");
			s.setBucket("ffs");
			s.setDepartment("fdsfsd");
			s.setLoginName("test1");
			s.setPwd("12321");
			s.setEndpoint("sfdfsd");
			baseDao.save(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Action("b")
	public void b(){
		try {
			SysUser  u = userService.getUserByLoginName("test");
			System.out.println(u.getAuths().get(0).getAccMethod());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	@Action("security")
	public void ttt(){
		long begin = System.currentTimeMillis();
		AssumeRoleResponse ossres = new OssSTSService().getSTS();
		
		OssSecurityResponse res = new OssSecurityResponse();
		res.setAccessKeyId(ossres.getCredentials().getAccessKeyId());
		res.setAccessKeySecret(ossres.getCredentials().getAccessKeySecret());
		res.setSecurityToken(ossres.getCredentials().getSecurityToken());
		res.setHost("http://testofwjt.oss-cn-shanghai.aliyuncs.com");
		res.setKey("/public/20160127/12312312-");
		System.out.println("=============get sts success");
		try {
			ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
			ServletActionContext.getResponse().getWriter().write(JsonMapper.buildNonDefaultMapper().toJson(res));
			ServletActionContext.getResponse().getWriter().flush();
			System.out.println("get security success");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 long end = System.currentTimeMillis();
	      System.out.println("server耗时:"+(end-begin));
	}

	
	
}
